package gui.menu;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

import game.buildings.*;
import main.Antiquity;

/**
 *	@(#)LandMenu.java
 *
 *	The <code>LandMenu</code> class represents the context menu
 *	in the countryside. It provides methods to add building options
 *	for the various land buildings.
 *
 *	@author Grzegorz Kobiela
 *	@version 1.0 2008/5/15
 */
public final class LandMenu
extends JPopupMenu
{
	/** Constant representing the 'inn' option. */
	public static final int OPTION_INN = 0;

	/** Constant representing the 'woodcutter' option. */
	public static final int OPTION_WOODCUTTER = 1;

	/** Constant representing the 'mine' option. */
	public static final int OPTION_MINE = 2;

	/** Constant representing the 'farm' option. */
	public static final int OPTION_FARM = 3;

	/** Constant representing the 'pollution' option. */
	public static final int OPTION_POLLUTION = 4;

	/** The menu items. */
	private JMenuItem[] items;

	/** The location. */
	private Point location;

	/**
	 *	Creates a new <code>LandMenu</code> instance.
	 */
	public LandMenu() {
		super();
		items = new JMenuItem[5];

		/* Inn option */
		add(items[OPTION_INN] = new JMenuItem(Antiquity.get("cmInn")));
		items[OPTION_INN].addActionListener(
			new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					Inn inn = new Inn(Antiquity.getPlayer().getColor());
					if(Antiquity.checkPurchase(Antiquity.getPlayer(), inn, false)) Antiquity.checkPlacement(inn, location);
				}
			}
		);
		addSeparator();

		/* Woodcutter option */
		add(items[OPTION_WOODCUTTER] = new JMenuItem(Antiquity.get("cmWoodcutter")));
		items[OPTION_WOODCUTTER].addActionListener(
			new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					Woodcutter woodcutter = new Woodcutter();
					if(Antiquity.checkPurchase(Antiquity.getPlayer(), woodcutter, false)) Antiquity.checkPlacement(woodcutter, location);
				}
			}
		);

		/* Mine option */
		add(items[OPTION_MINE] = new JMenuItem(Antiquity.get("cmMine")));
		items[OPTION_MINE].addActionListener(
			new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					Mine mine = new Mine();
					if(Antiquity.checkPurchase(Antiquity.getPlayer(), mine, false)) Antiquity.checkPlacement(mine, location);
				}
			}
		);

		/* Farm option */
		add(items[OPTION_FARM] = new JMenuItem(Antiquity.get("cmFarm")));
		items[OPTION_FARM].addActionListener(
			new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					Farm farm = new Farm();
					if(Antiquity.checkPurchase(Antiquity.getPlayer(), farm, false)) Antiquity.checkPlacement(farm, location);
				}
			}
		);
		addSeparator();

		/* Pollution removal option */
		add(items[OPTION_POLLUTION] = new JMenuItem(Antiquity.get("cmPollution")));
		items[OPTION_POLLUTION].addActionListener(
			new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					Antiquity.clean(location);
				}
			}
		);
	}

	/**
	 *	Returns all menu items.
	 *	@return all menu items
	 */
	public JMenuItem[] getItems() {
		return items;
	}

	/**
	 *	Displays the popup menu at the given location in the
	 *	coordinate space of the given component.
	 *	@param invoker the component
	 *	@param location the location
	 */
	public void show(Component invoker, Point location) {
		setVisible(true);
		setLocation(location);
		show(invoker, location.x, location.y);
	}

	/**
	 *	Sets the action location.
	 *	@param location the new location
	 */
	public void setLocation(Point location) {
		this.location = game.LandSystem.getPoint(location);
	}
}